package com.jiyi.activity.datainput.sm;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiyi.bean.datainput.new_input.BaseCheck;
import com.jiyi.bean.datainput.new_input.FireSpaceBean;
import com.jiyi.exception.SampleProgressObserver;
import com.jiyi.retrofit.HttpMethods;
import com.jiyi.widget.NoScrollGridView;
import com.jiyi.widget.picselect.PicsAdapter;
import com.jiyi.widget.picshow.ImagePagerActivity;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.utils.RadioGroupUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/3/29.
 */

public class FireSpaceReadActivity extends ActivityBase {
    @BindView(R.id.tvBack)
    TextView tvBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvSearch)
    TextView tvSearch;
    @BindView(R.id.rbSettingYes)
    RadioButton rbSettingYes;
    @BindView(R.id.rbSettingNo)
    RadioButton rbSettingNo;
    @BindView(R.id.rgSettingIsOK)
    RadioGroup rgSettingIsOK;
    @BindView(R.id.tvSettingProblem)
    TextView tvSettingProblem;
    @BindView(R.id.tvSettingMeasure)
    TextView tvSettingMeasure;
    @BindView(R.id.rbMarkYes)
    RadioButton rbMarkYes;
    @BindView(R.id.rbMarkNo)
    RadioButton rbMarkNo;
    @BindView(R.id.rgMarkIsOK)
    RadioGroup rgMarkIsOK;
    @BindView(R.id.tvMarkProblem)
    TextView tvMarkProblem;
    @BindView(R.id.tvMarkMeasure)
    TextView tvMarkMeasure;
    @BindView(R.id.rbObstacleYes)
    RadioButton rbObstacleYes;
    @BindView(R.id.rbObstacleNo)
    RadioButton rbObstacleNo;
    @BindView(R.id.rgHasObstacle)
    RadioGroup rgHasObstacle;
    @BindView(R.id.tvObstacleProblem)
    TextView tvObstacleProblem;
    @BindView(R.id.tvObstacleMeasure)
    TextView tvObstacleMeasure;
    @BindView(R.id.rbBlockYes)
    RadioButton rbBlockYes;
    @BindView(R.id.rbBlockNo)
    RadioButton rbBlockNo;
    @BindView(R.id.rgHasBlock)
    RadioGroup rgHasBlock;
    @BindView(R.id.tvBlockProblem)
    TextView tvBlockProblem;
    @BindView(R.id.tvBlockMeasure)
    TextView tvBlockMeasure;


    @BindView(R.id.ivHint)
    ImageView ivHint;
    @BindView(R.id.top_layout)
    RelativeLayout topLayout;
    @BindView(R.id.gvSetting)
    NoScrollGridView gvSetting;
    @BindView(R.id.gvMark)
    NoScrollGridView gvMark;
    @BindView(R.id.gvObstacle)
    NoScrollGridView gvObstacle;
    @BindView(R.id.gvBlock)
    NoScrollGridView gvBlock;

    String build_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fire_space_read);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        getIntentData();
        setTop();
        setViewStatus();

        if (!TextUtils.isEmpty(build_id)) {
            //修改时，获取数据
            getData();
        }
    }

    private void getIntentData() {
        build_id = getIntent().getStringExtra("build_no");
        if (TextUtils.isEmpty(build_id))
            build_id = "";
    }

    private void setTop() {
        tvTitle.setText("消防车道与消防扑救场地");
        tvSearch.setVisibility(View.GONE);
    }

    private void setViewStatus() {
        RadioGroupUtil.disableRadioGroup(rgHasBlock);
        RadioGroupUtil.disableRadioGroup(rgHasObstacle);
        RadioGroupUtil.disableRadioGroup(rgMarkIsOK);
        RadioGroupUtil.disableRadioGroup(rgSettingIsOK);
    }

    public void getData() {
        HttpMethods.getInstance().getFireLanesInfo(UserInfoUtil.getCurrentUserAccessToken(), build_id, 2)
                .subscribe(new SampleProgressObserver<FireSpaceBean>(this) {
                    @Override
                    public void onNext(FireSpaceBean fireSpaceBean) {
                        if (fireSpaceBean == null || "".equals(fireSpaceBean.getGson_null())) {
                            fireSpaceBean = new FireSpaceBean();
                            BaseCheck bc1 = new BaseCheck();
                            bc1.setIs_ok(0);
                            bc1.setProblem("");
                            bc1.setMeasures("");
                            fireSpaceBean.setSize_up(bc1);
                            BaseCheck bc2 = new BaseCheck();
                            bc2.setIs_ok(0);
                            bc2.setProblem("");
                            bc2.setMeasures("");
                            fireSpaceBean.setIs_flag(bc2);
                            BaseCheck bc3 = new BaseCheck();
                            bc3.setIs_ok(0);
                            bc3.setProblem("");
                            bc3.setMeasures("");
                            fireSpaceBean.setIs_barrier(bc3);
                            BaseCheck bc4 = new BaseCheck();
                            bc4.setIs_ok(0);
                            bc4.setProblem("");
                            bc4.setMeasures("");
                            fireSpaceBean.setIs_occupy(bc4);
                        }

                        fillData(fireSpaceBean);
                    }
                });
    }

    private void fillData(FireSpaceBean fireSpaceBean) {
        if (fireSpaceBean.getSize_up().getIs_ok() == 1) {
            rbSettingYes.setChecked(true);
        }

        tvSettingProblem.setText(fireSpaceBean.getSize_up().getProblem());
        tvSettingMeasure.setText(fireSpaceBean.getSize_up().getMeasures());

        if (fireSpaceBean.getIs_flag().getIs_ok() == 1) {
            rbMarkYes.setChecked(true);
        }

        tvMarkProblem.setText(fireSpaceBean.getIs_flag().getProblem());
        tvMarkMeasure.setText(fireSpaceBean.getIs_flag().getMeasures());

        if (fireSpaceBean.getIs_barrier().getIs_ok() == 1) {
            rbObstacleYes.setChecked(true);
        }

        tvObstacleProblem.setText(fireSpaceBean.getIs_barrier().getProblem());
        tvObstacleMeasure.setText(fireSpaceBean.getIs_barrier().getMeasures());

        if (fireSpaceBean.getIs_occupy().getIs_ok() == 1) {
            rbBlockYes.setChecked(true);
        }

        tvBlockProblem.setText(fireSpaceBean.getIs_occupy().getProblem());
        tvBlockMeasure.setText(fireSpaceBean.getIs_occupy().getMeasures());

        initGridViewList(fireSpaceBean);

    }

    void initGridViewList(FireSpaceBean fireSpaceBean)
    {
        if (fireSpaceBean.getSize_up().getImage() == null || fireSpaceBean.getSize_up().getImage().size() <= 0) {
            gvSetting.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvSetting,fireSpaceBean.getSize_up().getImage());
        }


        if (fireSpaceBean.getIs_flag().getImage() == null || fireSpaceBean.getIs_flag().getImage().size() <= 0) {
            gvMark.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvMark,fireSpaceBean.getIs_flag().getImage());
        }

        if (fireSpaceBean.getIs_barrier().getImage() == null || fireSpaceBean.getIs_barrier().getImage().size() <= 0) {
            gvObstacle.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvObstacle,fireSpaceBean.getIs_barrier().getImage());
        }

        if (fireSpaceBean.getIs_occupy().getImage() == null || fireSpaceBean.getIs_occupy().getImage().size() <= 0) {
            gvBlock.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvBlock,fireSpaceBean.getIs_occupy().getImage());
        }
    }

    void setGridviewAdapter(GridView gridView, List<String> picList)
    {
        gridView.setVisibility(View.VISIBLE);
        //图片列表
        if (picList != null) {
            final String [] urls = new String [picList.size()];
            for (int i = 0; i < picList.size(); i++) {
                urls[i] = picList.get(i);
            }

            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    Intent intent = new Intent(view.getContext(),ImagePagerActivity.class);
                    intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls);
                    intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
                    view.getContext().startActivity(intent);
                }
            });
            PicsAdapter picsAdapter = new PicsAdapter(gridView.getContext(),picList);
            gridView.setAdapter(picsAdapter);
        }
    }

    @OnClick({R.id.tvBack})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvBack:
                finish();
                break;
            default:
                break;
        }
    }
}
